package com.group01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group01.enums.CodeAndMessageEnum;
import com.group01.mapper.CommodityMapper;
import com.group01.pojo.Commodity;
import com.group01.pojo.ShopCar;
import com.group01.result.Result;
import com.group01.service.ShopCarService;
import com.group01.mapper.ShopCarMapper;
import com.group01.utils.CommonUtils;
import com.group01.vo.ShopCar.ShopCarGetInfoVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 19559
* @description 针对表【es_shopCar(购物车表)】的数据库操作Service实现
* @createDate 2024-06-23 08:45:12
*/
@Service
public class ShopCarServiceImpl extends ServiceImpl<ShopCarMapper, ShopCar> implements ShopCarService {

    /**
     * 注入
     */
    private final ShopCarMapper shopCarMapper;
    private final CommodityMapper commodityMapper;

    public ShopCarServiceImpl(ShopCarMapper shopCarMapper, CommodityMapper commodityMapper) {
        this.shopCarMapper = shopCarMapper;
        this.commodityMapper = commodityMapper;
    }

    /**
     * 查询当前用户的购物车列表
     * @return {@link Result }<{@link List }<{@link ShopCarGetInfoVO }>>
     */
    @Override
    public Result<List<ShopCarGetInfoVO>> getShopCarInfoList() {
        QueryWrapper<ShopCarGetInfoVO> queryWrapper = new QueryWrapper<>();
        // 获取用户id
        Integer userId = CommonUtils.getLocalId();
        queryWrapper.eq("a.user_id" , userId);

        List<ShopCarGetInfoVO> list = shopCarMapper.getShopCarInfoList(queryWrapper);

        return Result.success(CodeAndMessageEnum.FIND_RESULT , list);
    }

    /**
     * 添加购物车
     * @param id 商品id
     * @return {@link Result }<{@link String }>
     */
    @Override
    public Result<String> AddShopCarInfo(Integer id, Integer num) {
        Long count = commodityMapper.selectCount(new LambdaQueryWrapper<Commodity>().eq(Commodity::getId , id));
        if(count == 0) {
            return Result.error(CodeAndMessageEnum.FIND_COMMODITY_ID_FAIL);
        }
        // 获取用户id
        Integer userId = CommonUtils.getLocalId();
        ShopCar car = shopCarMapper.selectOne(new LambdaQueryWrapper<ShopCar>().eq(ShopCar::getUserId , userId).eq(ShopCar::getGoodsId , id));
        if(car != null) { // 商品存在,那么更新数量
            shopCarMapper.updateShopCarNum(userId , id, num);
        } else {
            shopCarMapper.insert(new ShopCar().setUserId(userId).setGoodsId(id).setNum(num));
        }
        return Result.success(CodeAndMessageEnum.SUCCESS_ADD_SHOP);
    }

    /**
     * 修改购物车数量根据购物车主键
     * @param id 购物车主键
     * @param num 商品加购数量
     * @return {@link Result }<{@link String }>
     */
    @Override
    public Result<String> updateShopCarNums(Integer id, Integer num) {
        /*
         * 检查id
         */
        if(shopCarMapper.checkShopCarId(id) == 0) {
            return Result.error(CodeAndMessageEnum.FIND_SHOP_ID_FAIL);
        }
        shopCarMapper.updateShopCarNumByPrimaryKey(id , num);
        return Result.success(CodeAndMessageEnum.SUCCESS_ADD_NUMS);
    }

    /**
     * 删除与批量删除购物车信息
     * @param ids 购物车主键集合
     * @return {@link Result }<{@link String }>
     */
    @Override
    public Result<String> deleteShopCarInfos(List<Integer> ids) {
        shopCarMapper.deleteBatchIds(ids);
        return Result.success(CodeAndMessageEnum.DELETE_SUCCESS);
    }

    /**
     * 获取当前
     * @return {@link Result }<{@link Integer }>
     */
    @Override
    public Result<Long> getCartSize() {
        /*
         * 获取用户id
         */
        Integer userId = CommonUtils.getLocalId();

        LambdaQueryWrapper<ShopCar> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ShopCar::getUserId , userId);

        Long count = shopCarMapper.selectCount(queryWrapper);

        return Result.success(CodeAndMessageEnum.FIND_RESULT , count);
    }

}




